package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.util.StringUtils;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DIshController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    //http://localhost:8081/dish/list?categoryId=1397844263642378242&status=1
    @GetMapping("/list")
    public ResultInfo selectByCategoryId(Long categoryId,Integer status){
        String key ="dish_"+categoryId;
        List<Dish> dishList = (List<Dish>)redisTemplate.opsForValue().get(key);
        if (CollectionUtil.isEmpty(dishList)){
            dishList= dishService.selectByCategoryId(categoryId,status);
            redisTemplate.opsForValue().set(key,dishList);
        }
        return ResultInfo.success(dishList);
    }
}
